﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BuffInfo : MonoBehaviour
{
    public static BuffInfo _instance;

    //public Dictionary<int, Buff> buff_dic = new Dictionary<int, Buff>();
    public List<Buff> buff_dic = new List<Buff>();

    public TextAsset bufftext;
    public static string Path_Asset = "Assets/Resources/Buff.txt";

    private void Awake()
    {
        _instance = this;
        ReadInfo();
        //Debug.Log(buff_dic.Count);
        //Debug.Log(getBuffInfobyid(101).battle_update);
    }

    public Buff getBuffInfobyid(int id)
    {
        Buff info = null;
        //buff_dic.TryGetValue(id, out info);
        int front = 0;
        int after = buff_dic.Count -1 ;
        int mid;
        while (front <= after)
        {
            mid = (front + after) / 2;

            if (buff_dic[mid].id == id)
            {
                info = buff_dic[mid];
                break;
            }
            else if (buff_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return info;
    }
    public int Check_ID_if_Exist(int id)
    {
        int front = 0;
        int after = buff_dic.Count - 1;
        int mid;
        while (front <= after)
        {
            mid = (front + after) / 2;

            if (buff_dic[mid].id == id)
            {
                return mid;
            }
            else if (buff_dic[mid].id > id)
            {
                after = mid - 1;
                continue;
            }
            else
            {
                front = mid + 1;
                continue;
            }
        }
        return -1;
    }

    public void Sort_BUFF_Dic()
    {
        buff_dic.Sort(
            (left, right) =>
            {
                if (left.id > right.id) return 1;
                else if (left.id == right.id) return 0;
                else return -1;
            });
    }

    public int CreateNewBuff()
    {
        int buff_id = 101;
        while (Check_ID_if_Exist(buff_id) >= 0) buff_id++;// 找到一个不存在的ID

        Buff info = new Buff(buff_id);
        buff_dic.Add(info);

        return buff_id;
    }

    public void ReadInfo()
    {
        buff_dic.Clear();

        string text = bufftext.text;
        string[] strArray = text.Split('\n');

        foreach(string str in strArray)
        {
            string[] proArray = str.Split(',');
            Buff buff = new Buff(int.Parse(proArray[0]));
          
            buff.tag = proArray[1];
            buff.lasttime = float.Parse(proArray[2]);
            buff.bleed_interval = float.Parse(proArray[3]);
            buff.bleed_rest = buff.bleed_interval;
            buff.battle_update = float.Parse(proArray[4]);
            buff.vol_update = float.Parse(proArray[5]);
            buff.radiation_update = float.Parse(proArray[6]);
            buff_dic.Add(buff);
        }
    }

    public void WriteInfo()
    {
        string result = "";
        foreach(Buff one_pair in buff_dic)
        {
            string one_line = "";
            one_line += one_pair.id.ToString();
            one_line += ",";
            one_line += one_pair.tag;
            one_line += ",";
            one_line += one_pair.lasttime.ToString();
            one_line += ",";
            one_line += one_pair.bleed_interval.ToString();
            one_line += ",";
            one_line += one_pair.battle_update.ToString();
            one_line += ",";
            one_line += one_pair.vol_update.ToString();
            one_line += ",";
            one_line += one_pair.radiation_update.ToString();
            one_line += ",";
            result += one_line;
            result += "\n";
        }
        result = result.Remove(result.Length - 1);
        System.IO.File.WriteAllText(Path_Asset, result);
    }
    public string[] ToList()
    {
        string[] result;
        result = new string[buff_dic.Count + 1];
        result[0] = "none";
        int index = 1;

        foreach(Buff buff in buff_dic)
        {
            string info_line = "";
            info_line += "[ID" + buff.id.ToString() +"]" + buff.tag.ToString();

            result[index] = info_line;
            index++;
        }


        return result;
    }
}

public class Buff
{
    public int id;//id
    public string tag;//标签
    public float lasttime;//默认持续时间
    public float bleed_interval;//流血间隔
    public float battle_update;//战力修正
    public float vol_update;//移速修正
    public float radiation_update;//接受辐射修正

    public float bleed_rest;// 掉血间隔

    public Buff(int id)
    {
        this.id = id;
        this.tag = "default";
        this.lasttime = 60;
        this.bleed_interval = 0;
        this.battle_update = 0;
        this.vol_update = 0;
        this.radiation_update = 0;
    }

    public void activate(float deltaTime)
    {
        lasttime -= deltaTime;
        Debug.Log("buff:" + this.tag + "  持续时间：" + this.lasttime);
        if(bleed_interval > 0.001f)// 设置个阈值
        {
            bleed_rest -= deltaTime;
            if(bleed_rest < 0)// 一旦到了时间就掉一滴血
            {
                PlayerController._instance.HP_Change(-1.0f);
                bleed_rest = bleed_interval;
            }
        }
        if(battle_update > 0.001f)
        {
            int Battle_Percent = (int)(100 * battle_update);
            PlayerController._instance.Attack_Change(Battle_Percent);
        }
        if(vol_update > 0.001f)
        {
            int Move_Percent = (int)(100 * vol_update);
            PlayerController._instance.MoveSpeed_Change(Move_Percent);
        }
        if(radiation_update >= 0)
        {
            PlayerController._instance.Change_Radiation_Rate(radiation_update);
        }
    }

    public bool isEnd()
    {
        return lasttime < 0;
    }

    public List<string> BriefInfo()
    {
        List<string> result = new List<string>();
        result.Add("标签：" + this.tag);
        result.Add("持续时间：" + this.lasttime + "s");
        if (this.bleed_interval != 0) result.Add("流血：" + "流血间隔(s):" + this.bleed_interval.ToString());
        if (this.battle_update != 0) result.Add("战斗力修正(%)：" + this.battle_update.ToString() + "%");
        if (this.vol_update != 0) result.Add("速度修正(%)：" + this.vol_update.ToString() + "%");
        if (this.radiation_update != 0) result.Add("接受辐射能力修正:" + this.radiation_update.ToString() + "%");

        return result;
    }
}